<template>
    <!-- 头部 -->
    <van-nav-bar title="商品详情" right-text="按钮" left-arrow @click-left="onClickLeft">
        <template #right>
            <van-icon name="ellipsis" />
        </template>
    </van-nav-bar>
    <div class="product-info">
        <img :src="info.goodsCoverImg" alt="" style="width: 100%;">
        <div class="info-goodsName">
            {{ info.goodsName }}
        </div>
        <p class="info-p">免邮费 顺丰包邮</p>
        <div class="info-price">
            ￥{{ info.sellingPrice }}
        </div>
        <div class="info-ul">
            <li>概述</li>
            <li>参数</li>
            <li>安装服务</li>
            <div class="info-ul-li">常见问题</div>
        </div>
        <div v-html="info.goodsDetailContent" class="info-content"></div>
    </div>
    <!-- 底部加入购物车 -->
    <van-action-bar>
        <van-action-bar-icon icon="chat-o" text="客服" />
        <van-action-bar-icon icon="cart-o" text="购物车" :badge='goodsCounts' />
        <van-action-bar-button type="warning" text="加入购物车" @click="addProduct" />
        <van-action-bar-button type="danger" text="立即购买" />
    </van-action-bar>
</template>

<script setup>
import { useRoute } from 'vue-router'
import { getInfoData, addCart } from '@/api/index'
import { onMounted, reactive, computed, ref } from 'vue'
import { showSuccessToast } from 'vant';
import cartStore from '@/stores/cartStore'
let store = cartStore();
let route = useRoute();
let info = reactive({})
let goodsCounts = ref(0)
// 获取商品数量  这个方法不好 最好还是在仓库中获得
let goodsNumber = () => {
    let goodsCount = 0;
    let p = new Promise((resolve, reject) => {
        let res = store.getCartListAsync();
        resolve(res)
    }).then(res => {
        let { data, resultCode } = res;
        if (resultCode == 200) {
            data.forEach(item => {
                goodsCount += item.goodsCount

            })
            goodsCounts.value = goodsCount
        }
    })
}
goodsNumber()




onMounted(() => {
    getData()
})
let getData = async (id) => {
    let { data } = await getInfoData(route.params.productId)
    for (const key in data) {
        info[key] = data[key]
    }
}
const onClickLeft = () => history.back();
const onClickRight = () => showToast('按钮');

// 加入购物车 
let addProduct = async () => {
    let { message, resultCode } = await addCart({ goodsId: route.params.productId, goodsCount: 1 })
    goodsNumber()
    if (resultCode == 200) {
        showSuccessToast('添加成功');

    } else {
        showSuccessToast(message)
    }
}
</script>

<style lang="less" scoped>
@--van-nav-bar-height: 70px;

.van-nav-bar {
    position: fixed;
    width: 100%;
    --van-nav-bar-height: 70px;
    top: 0;
    left: 0;
    line-height: 70px;
    box-sizing: border-box;
    font-size: 27px;
    background: #fff;
    --van-nav-bar-title-font-size: 24px;
    font-weight: normal;
    border-bottom: 1px solid #ccc;
    overflow: hidden;
}

.product-info {
    margin-top: 70px;
    padding: 0 20px;
    margin-bottom: 80px;

    .info-goodsName {
        font-size: 26px;
    }

    .info-p {
        color: #999999;
        font-size: 20px;
        margin-bottom: 10px;
    }

    .info-price {
        color: red;
        font-size: 24px;
    }

    .info-ul {
        list-style: none;
        display: flex;
        margin: 30px 0;
        height: 50px;
        align-items: center;

        li {
            height: 50px;
            width: 25%;
            text-align: center;
            font-size: 23px;
            border-right: 1px solid #ccc;
            line-height: 50px;
        }

        .info-ul-li {
            width: 25%;
            text-align: center;
            font-size: 23px;
            height: 50px;
            line-height: 50px;
        }
    }
}

.van-action-bar {
    padding: 0 20px;
    position: fixed;
    bottom: 0;
    left: 0;
    height: 80px;

    .van-action-bar-icon {
        width: 120px;
    }
}
</style>